package ucl.cs.bank.server;

/**
 * UCLAccount Implementation.
 * @author  K. Bryson.
 * @version 1.0
 */
///////////////// WHY DOES THIS NOT WORK CORRECTLY ? //////////////////
import java.rmi.RemoteException;
import ucl.cs.bank.common.*;

public class UCLAccountImpl extends java.rmi.server.UnicastRemoteObject implements UCLAccount {

   // This supports serialization.
   static final long serialVersionUID = 1;
   
   private String name;
   private int    number;
   private float  balance = 0.0f;

   public UCLAccountImpl(String name, int account_number) throws RemoteException {
      super();
      this.name   = name;
      this.number = account_number;
   }

   public synchronized void deposit(float amount) throws RemoteException {
      System.out.println(name + ": Depositing " + amount);
      float tmp = balance + amount;
      threadRest();
      balance = tmp;
   }

   public synchronized void withdraw(float amount) throws RemoteException {
      System.out.println(name + ": Withdrawing " + amount);
      float tmp = balance - amount;
      threadRest();
      balance = tmp;
   }


   public String getName() {
      return name;
   }

   public int getNumber() {
      return number;
   }

   public float getBalance() {
      return balance;
   }

   public synchronized String getDescription() throws RemoteException {
      return " Account number: "  + number +
             "\n Account name: " + name + 
             "\n Balance: "      + balance;
   }

   private void threadRest() {
      try {
         Thread.sleep(10000);
      }
      catch (InterruptedException except) {
      }
   }
}
